From c9366090153e8aba3a673b2b77cbc8acc24e59a5 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 15 Dec 2023 17:15:32 +0700
Subject: Update promotion program feature

---
 .../product-variant/[id]/promotion/highlight.tsx   | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx

(limited to 'src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx')

diff --git a/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
new file mode 100644
index 00000000..0fe8fd1b
--- /dev/null
+++ b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
@@ -0,0 +1,56 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { SolrResponse } from "~/common/types/solr";
+
+const SOLR_HOST = process.env.SOLR_HOST as string
+
+export default async function handler(req: NextApiRequest, res: NextApiResponse) {
+  const productId = req.query.id as string
+
+  if (req.method === 'GET') {
+    const types = ['bundling', 'discount_loading', 'merchandise']
+    const queryParams = new URLSearchParams({
+      q: `product_ids:${productId}`,
+      rows: '1'
+    })
+
+    let programs: any[] = []
+
+    for (const type of types) {
+      queryParams.set('fq', `type_value_s:${type}`)
+      const response = await fetch(`${SOLR_HOST}/solr/promotion_program_lines/select?${queryParams.toString()}`)
+      const data: SolrResponse<any[]> = await response.json()
+      programs.push(...data.response.docs)
+    }
+
+    programs = await extractPrograms(programs)
+    res.status(200).json({ data: programs })
+  }
+}
+
+const extractPrograms = async (programs: any[]) => {
+  const result = []
+
+  for (const program of programs) {
+    const data: any = {}
+
+    data.id = program.id
+    data.program_id = program.program_id_i
+    data.name = program.name_s
+    data.type = {
+      value: program.type_value_s,
+      label: program.type_label_s,
+    }
+    data.limit = program.package_limit_i
+    data.limit_user = program.package_limit_user_i
+    data.limit_trx = program.package_limit_trx_i
+    data.price = program.price_f
+    data.total_qty = program.total_qty_i
+
+    data.products = JSON.parse(program.products_s)
+    data.free_products = JSON.parse(program.free_products_s)
+
+    result.push(data)
+  }
+
+  return result
+}
\ No newline at end of file
-- 
cgit v1.2.3


From f62b2345f463695ef0f8f79830cd76b6e0332821 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Sat, 13 Jan 2024 10:35:22 +0700
Subject: Refactor src migrate folder

---
 src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx')

diff --git a/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
index 0fe8fd1b..8153f346 100644
--- a/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
+++ b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
@@ -1,5 +1,5 @@
 import { NextApiRequest, NextApiResponse } from "next";
-import { SolrResponse } from "~/common/types/solr";
+import { SolrResponse } from "~/types/solr";
 
 const SOLR_HOST = process.env.SOLR_HOST as string
 
-- 
cgit v1.2.3


From b841b265fe697e28e4d5ce47017910bf9b536de5 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Thu, 29 Feb 2024 11:13:58 +0700
Subject: Update get program line API only active is true

---
 src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

(limited to 'src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx')

diff --git a/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
index 8153f346..c4acacf1 100644
--- a/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
+++ b/src-migrate/pages/api/product-variant/[id]/promotion/highlight.tsx
@@ -16,7 +16,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
     let programs: any[] = []
 
     for (const type of types) {
-      queryParams.set('fq', `type_value_s:${type}`)
+      queryParams.set('fq', `type_value_s:${type}`) 
+      queryParams.append('fq', `active_b:true`)
+
       const response = await fetch(`${SOLR_HOST}/solr/promotion_program_lines/select?${queryParams.toString()}`)
       const data: SolrResponse<any[]> = await response.json()
       programs.push(...data.response.docs)
-- 
cgit v1.2.3

